package com.cs.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cs.domain.entity.Order;
import com.cs.domain.entity.ShopCar;
import com.cs.domain.entity.UserOrder;
import com.cs.domain.vo.OrderVo;
import com.cs.store.dao.IOrderDao;
import com.cs.store.dao.IOrderVoDao;
import com.cs.store.dao.IShopCarDao;
import com.cs.store.dao.IUserOrderDao;
import com.cs.store.service.IOrderService;
import com.cs.user.dao.IUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderVoDao iOrderVoDao;
    @Autowired
    private IShopCarDao iShopCarDao;
    @Autowired
    private IOrderDao iOrderDao;
    @Autowired
    private IUserOrderDao iUserOrderDao;
    @Autowired
    private IUserDao iUserDao;


    /**
     * 根据userid查看购物车
     *
     * @param
     * @return
     */


    @Override
    public List<ShopCar> findByuserId(String username) {
        QueryWrapper Qwrapper = new QueryWrapper<>();
        Qwrapper.eq("user_username", username);
        Long userId = iUserDao.selectOne(Qwrapper).getId();

        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("car_user_id", userId);
        return iShopCarDao.selectList(wrapper);
    }


    /**
     * 购物车商品添加到订单表
     *
     * @param
     * @return
     */
    @Override
    public Integer payOrder(String username, Long storeId, String orderNum,String payPrice) {
        QueryWrapper Qwrapper = new QueryWrapper<>();
        Qwrapper.eq("user_username", username);
        Long userId = iUserDao.selectOne(Qwrapper).getId();


        int count = 0;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("car_user_id", userId);
        wrapper.eq("car_store_id", storeId)
        ;
        //查找购物车中商品
        List<ShopCar> shopCars = iShopCarDao.selectList(wrapper);
        //添加到订单表
        shopCars.forEach(shopCar -> {
            Order order = new Order();   //订单表
            UserOrder userOrder = new UserOrder();  //个人订单表
            order.setOrderNum(orderNum);
            order.setUserId(userId);
            order.setStoreId(storeId);
            order.setGoodsId(shopCar.getGoodsId());
            order.setGoodsNum(shopCar.getCount());
            order.setOriginalPrice(shopCar.getPrice());
            order.setPaidPrice(Double.valueOf(payPrice));
            order.setOrderState("待支付");
            order.setPayType(1L);
            iOrderDao.insert(order);
//
            userOrder.setOrderNum(orderNum);
            userOrder.setUserId(userId);
            userOrder.setStoreId(storeId);
            userOrder.setGoodsId(shopCar.getGoodsId());
            userOrder.setGoodsNum(shopCar.getCount());
            userOrder.setOriginalPrice(shopCar.getPrice());
            userOrder.setPaidPrice(Double.valueOf(payPrice));
            userOrder.setOrderState("待支付");
            userOrder.setPayType(1L);
            iUserOrderDao.insert(userOrder);
        });
//        iShopCarDao.delete(wrapper);
        return ++count;
    }

    @Override
    public Integer delCarAfOrder(String orderNum) {

        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("order_num", orderNum);
          List<Order> findorders = iOrderDao.selectList(wrapper);

        QueryWrapper wrapperCar = new QueryWrapper<>();
        wrapper.eq("car_user_id", findorders.get(0).getUserId());
        wrapper.eq("car_store_id", findorders.get(0).getStoreId());
        iShopCarDao.delete(wrapperCar);
        return 1;
    }


    /**
     * 根据userid查看个人订单详情
     *
     * @param username
     * @return
     */
    @Override
    public List<OrderVo>  findUserOrder(String username) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("user_username", username);
        return iOrderVoDao.selectList(wrapper);
    }

    @Override
    public List<OrderVo> findStoreOrder(String storeName) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("store_name", storeName);
        return iOrderVoDao.selectList(wrapper);
    }

    @Override
    public List<OrderVo> oneUserOrder(String orderNum) {
        LambdaQueryWrapper<OrderVo> wrapper = new LambdaQueryWrapper();
        wrapper.eq(OrderVo::getOrderNum,orderNum);
        return iOrderVoDao.selectList(wrapper);
    }

    /**
     * 根据orderid 删除个人订单详情
     *
     * @param orderId
     * @return
     */
    @Override
    public Integer delUserOrder(Long orderId) {
        int del = iUserOrderDao.deleteById(orderId);
        return del;
    }


    @Override
    public Integer updateOrder(String state, String orderNum) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("order_num", orderNum);

        List<Order> orders = iOrderDao.selectList(wrapper);
        orders.forEach(order -> {
            order.setOrderState(state);
            iOrderDao.updateById(order);
        });
        List<UserOrder> userOrders = iUserOrderDao.selectList(wrapper);
        userOrders.forEach(userorder -> {
            userorder.setOrderState(state);
            iUserOrderDao.updateById(userorder);
        });
        return 1;
    }


}
